{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c9d791cd",
   "metadata": {},
   "source": [
    "# Numpy ndarray对象\n",
    "\n",
    "- `numpy`定义了一个n维数组对象，简称`ndarray`对象，它是一个一系列`相同`类型元素组成的数组集合。数组中的每个元素都占有大小相同的内存块。\n",
    "- `ndarray`对象采用了数组的`索引机制`，将数组中的每个元素映射到内存块上，并且按照一定的布局对内存块进行排列。\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f6bdf8c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "429cfc06",
   "metadata": {},
   "source": [
    "# numpy创建数组\n",
    "\n",
    "```python\n",
    "numpy.array(object, dtype = None, copy = True, Order = None, subok = False, ndmin = 0)\n",
    "```\n",
    "\n",
    "参数：\n",
    "\n",
    "| 序号 | 参数   | 描述说明                                                     |\n",
    "| ---- | ------ | :----------------------------------------------------------- |\n",
    "| 1    | object | 表示一个数组序列                                             |\n",
    "| 2    | dtype  | 可选参数，通过它可以改变数组的数据类型                       |\n",
    "| 3    | copy   | 可选，当数据源是ndarray是表示数组能否被复制，默认是True                  |\n",
    "| 4    | Order  | 可选，以哪种内存布局创建数组，有3个可选值，分别是C（行序列）/F（列序列）/A（默认） |\n",
    "| 5    | ndmin  | 可选，用于指定数组维数                                       |\n",
    "| 6    | subok  | 可选，类型为bool，默认为False。为True时，使用object的内部数据类型；为False时，使用object数组的数据类型。 |\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "80e0e2d4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([1,2,3,4,5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "23765c8e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(np.array([1,2,3,4,5]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2a5f53db",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array((1,2,3,4,5)) # 元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1dfd0e04",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(np.array((1,2,3,4,5)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7f23e737",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1,2,3,4,5])\n",
    "\n",
    "b = np.array(a)\n",
    "\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "87ef2972",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array(range(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "51ee4c3b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  4,  9, 16, 25, 36, 49, 64, 81])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([i**2 for i in range(10)]) # 里面是列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "555b4a49",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 2, 4, 6, 8])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建10以内的偶数的数组\n",
    "np.array([i for i in range(10) if i%2 == 0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ca456bbf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  2,  4,  6,  8, 10])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第二种做法\n",
    "np.array(range(0,11,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5e091abc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['1', '1.4', '5'], dtype='<U32')"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([1,1.4,'5'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8f495482",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([['1', 'b', '3'],\n",
       "       ['1', 'b', '3']], dtype='<U21')"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 二维数组(可以嵌套，列表元组都可以)\n",
    "np.array([\n",
    "    [1,'b',3],\n",
    "    ('1','b','3')\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "90c6947c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 注意嵌套序列数量不一会怎么样\n",
    "ar4 = np.array([[1,2,3],[1,2,3]]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d664c9e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [1, 2, 3]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ar4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d94a638f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 3)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ar4.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85fd002b",
   "metadata": {},
   "source": [
    " 1. 设置dtype参数，默认自动识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "aa9b5b40",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1,2,3,4,5])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "857338e8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2., 3., 4., 5.])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_dtype = np.array(a, dtype = 'float')\n",
    "a_dtype"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "675111bd",
   "metadata": {},
   "source": [
    "## 思考如何将浮点型的数据，设置为整形，会是什么情况？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c039e4b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 1])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([1.1,2.2,3,1.2], dtype = 'int')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51631ec6",
   "metadata": {},
   "source": [
    " 2.设置copy参数,默认为True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "ff9bf818",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([1,2,3,4,5])\n",
    "b = np.array(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "3891d931",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a: 4442187504 b: 4442187600\n"
     ]
    }
   ],
   "source": [
    "print('a:',id(a),'b:',id(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "8cdd9aaf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a: 4442187984   b: 4442187984\n",
      "以上看出a和b的内存地址\n",
      "a: [10  2  3  4  5]   b: [10  2  3  4  5]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([1,2,3,4,5])\n",
    "# 定义b，当设置copy参数为Fasle时，不会创建副本，\n",
    "# 两个变量会指向相同的内容地址，没有创建新的对象\n",
    "b = np.array(a, copy=False)\n",
    "# 输出a和b的id\n",
    "print('a:', id(a), '  b:', id(b))\n",
    "print('以上看出a和b的内存地址')\n",
    "# 由于a和b指向的是相同的内存地址，因此当修改b的元素时，a会发生变化\n",
    "b[0] = 10\n",
    "print('a:',a,'  b:',b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f402f38f",
   "metadata": {},
   "source": [
    "当默认为True时，表明是独立的复制，而不是引用；\n",
    "为False时，表明是引用"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "628631fe",
   "metadata": {},
   "source": [
    "3. ndmin 用于指定数组的维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "b33aab3f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1,2,3])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "a7560734",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.array(a, ndmin = 2)\n",
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d5b76df",
   "metadata": {},
   "source": [
    "4.subok参数，类型为bool值，默认False。为True，使用object的内部数据类型；False：使用object数组的数据类型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "69c35a42",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.matrix'>\n",
      "at,subok为True: <class 'numpy.matrix'>\n",
      "af,subok为False: <class 'numpy.ndarray'>\n",
      "4442264000 4442263872\n"
     ]
    }
   ],
   "source": [
    "# 创建一个矩阵\n",
    "a = np.mat([1,2,3,4])\n",
    "# 输出为矩阵类型\n",
    "print(type(a))\n",
    "\n",
    "#既要复制一份副本，又要保持原类型\n",
    "at = np.array(a,subok=True)\n",
    "af = np.array(a)  # 默认为False\n",
    "print('at,subok为True:',type(at))\n",
    "print('af,subok为False:',type(af))\n",
    "print(id(at),id(a))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "134e898c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4442231984 4442189424\n"
     ]
    }
   ],
   "source": [
    "a = np.array([1,2,3,4])\n",
    "c = a.copy() # copy使用的多一点\n",
    "print(id(a),id(c))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a37be44a",
   "metadata": {},
   "source": [
    "\n",
    "# arange()生成区间数组\n",
    "根据 start 与 stop 指定的范围以及 step 设定的步长，生成一个 ndarray。\n",
    "\n",
    "```python\n",
    "numpy.arange(start, stop, step, dtype)\n",
    "```\n",
    "参数说明   \n",
    "\n",
    "| 序号 | 参数 | 描述说明 |\n",
    "| :--- | :--- | :--- |\n",
    "| 1      | start| 起始值，默认为0   |\n",
    "| 2   | stop | 终止值（不包含）        |\n",
    "| 3      | step | 步长，默认为1        |\n",
    "| 4      | dtype |  返回ndarray的数据类型，如果没有提供，则会使用输入数据的类型。     |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "0852c552",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "a9a9b749",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 1., 2., 3.])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(3.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "a3af1f2b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "range(0, 3)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "range(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "76641851",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 1., 2., 3.])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(4,dtype = 'float')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "7850a889",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42,\n",
       "       44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76,\n",
       "       78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(10,100,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "7a82f547",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([    0,     1,     2, ..., 99997, 99998, 99999])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(100000) # 只打印边边角角"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "44b128a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  3,  6,  9, 12, 15, 18])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(20,step = 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "4e0f2e36",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.arange(200+1,step = 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "265a3e3f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "67"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc801c05",
   "metadata": {},
   "source": [
    "## 如何防止 float 不精确影响numpy.arange\n",
    "> <font color=red>注意：ceil((stop - start)/step)确定项目数，小浮点不精确(stop = .400000001)可以向列表中添加意外值。</font>\n",
    "#### 想得到一个长度为3的、从0.1开始的、间隔为0.1的数组，想当然地如下coding，结果意料之外："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "a32e0ba1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.1, 0.2, 0.3, 0.4])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(0.1,0.5,0.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d3bf3a6",
   "metadata": {},
   "source": [
    "# linspace() 创建等差数列\n",
    "\n",
    "返回在间隔[开始，停止]上计算的num个均匀间隔的样本。<b>数组是一个等差数列构成</b>\n",
    "\n",
    "```python\n",
    "np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)\n",
    "```\n",
    "\n",
    "参数说明   \n",
    "\n",
    "| 序号 | 参数 | 描述说明 |\n",
    "| :--- | :--- | :--- |\n",
    "| 1      | start| 必填项，序列的起始值，  |\n",
    "| 2   | stop | 必填项，序列的终止值，如果endpoint为true，该值包含于数列中      |\n",
    "| 3      | num | 要生成的等步长的样本数量，默认为50      |\n",
    "| 4      | endpoint |  该值为 true 时，数列中包含stop值，反之不包含，默认是True。  | \n",
    "| 5      | retstep |  如果为 True 时，生成的数组中会显示步长，反之不显示。   | \n",
    "| 6      | dtype |  ndarray 的数据类型 | "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "6f5e9dc8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.        ,  1.18367347,  1.36734694,  1.55102041,  1.73469388,\n",
       "        1.91836735,  2.10204082,  2.28571429,  2.46938776,  2.65306122,\n",
       "        2.83673469,  3.02040816,  3.20408163,  3.3877551 ,  3.57142857,\n",
       "        3.75510204,  3.93877551,  4.12244898,  4.30612245,  4.48979592,\n",
       "        4.67346939,  4.85714286,  5.04081633,  5.2244898 ,  5.40816327,\n",
       "        5.59183673,  5.7755102 ,  5.95918367,  6.14285714,  6.32653061,\n",
       "        6.51020408,  6.69387755,  6.87755102,  7.06122449,  7.24489796,\n",
       "        7.42857143,  7.6122449 ,  7.79591837,  7.97959184,  8.16326531,\n",
       "        8.34693878,  8.53061224,  8.71428571,  8.89795918,  9.08163265,\n",
       "        9.26530612,  9.44897959,  9.63265306,  9.81632653, 10.        ])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(1,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "d2547303",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.        ,  1.47368421,  1.94736842,  2.42105263,  2.89473684,\n",
       "        3.36842105,  3.84210526,  4.31578947,  4.78947368,  5.26315789,\n",
       "        5.73684211,  6.21052632,  6.68421053,  7.15789474,  7.63157895,\n",
       "        8.10526316,  8.57894737,  9.05263158,  9.52631579, 10.        ])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(1,10,20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "8bdcc9f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(1,10,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "2fb044b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(1,10,10,dtype='int')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "17f6a7ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(1,10,10,retstep=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "aefd5382",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2., 3., 4., 5.])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.linspace(1,5,5)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "d8c63932",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.   , 2.025, 3.05 , 4.075, 5.1  ])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.linspace(1,5.1,5)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "b3f95a71",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1. , 1.8, 2.6, 3.4, 4.2])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(1,5,5,endpoint=False) # 不包含末尾值5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "02bb7484",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2., 3., 4., 5.])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(1,5,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "4a23ba27",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([2.  , 2.25, 2.5 , 2.75, 3.  ]), 0.25)"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.linspace(2.0,3.0,5,retstep=True) # 显示步长\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "55e93615",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "1b2d7311",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([2. , 2.2, 2.4, 2.6, 2.8]), 0.2)"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.linspace(2.0,3.0,5,endpoint=False,retstep=True)\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd1af63a",
   "metadata": {},
   "source": [
    "#### 等差数列 在线性回归经常作为样本集\n",
    "如：生成x_data，值为[0, 100]之间500个等差数列数据集合作为样本特征，根据目标线性方程y=3*x+2，生成相应的标签集合y_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "8654233c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  0.        ,   0.2004008 ,   0.4008016 ,   0.6012024 ,\n",
       "         0.80160321,   1.00200401,   1.20240481,   1.40280561,\n",
       "         1.60320641,   1.80360721,   2.00400802,   2.20440882,\n",
       "         2.40480962,   2.60521042,   2.80561122,   3.00601202,\n",
       "         3.20641283,   3.40681363,   3.60721443,   3.80761523,\n",
       "         4.00801603,   4.20841683,   4.40881764,   4.60921844,\n",
       "         4.80961924,   5.01002004,   5.21042084,   5.41082164,\n",
       "         5.61122244,   5.81162325,   6.01202405,   6.21242485,\n",
       "         6.41282565,   6.61322645,   6.81362725,   7.01402806,\n",
       "         7.21442886,   7.41482966,   7.61523046,   7.81563126,\n",
       "         8.01603206,   8.21643287,   8.41683367,   8.61723447,\n",
       "         8.81763527,   9.01803607,   9.21843687,   9.41883768,\n",
       "         9.61923848,   9.81963928,  10.02004008,  10.22044088,\n",
       "        10.42084168,  10.62124248,  10.82164329,  11.02204409,\n",
       "        11.22244489,  11.42284569,  11.62324649,  11.82364729,\n",
       "        12.0240481 ,  12.2244489 ,  12.4248497 ,  12.6252505 ,\n",
       "        12.8256513 ,  13.0260521 ,  13.22645291,  13.42685371,\n",
       "        13.62725451,  13.82765531,  14.02805611,  14.22845691,\n",
       "        14.42885772,  14.62925852,  14.82965932,  15.03006012,\n",
       "        15.23046092,  15.43086172,  15.63126253,  15.83166333,\n",
       "        16.03206413,  16.23246493,  16.43286573,  16.63326653,\n",
       "        16.83366733,  17.03406814,  17.23446894,  17.43486974,\n",
       "        17.63527054,  17.83567134,  18.03607214,  18.23647295,\n",
       "        18.43687375,  18.63727455,  18.83767535,  19.03807615,\n",
       "        19.23847695,  19.43887776,  19.63927856,  19.83967936,\n",
       "        20.04008016,  20.24048096,  20.44088176,  20.64128257,\n",
       "        20.84168337,  21.04208417,  21.24248497,  21.44288577,\n",
       "        21.64328657,  21.84368737,  22.04408818,  22.24448898,\n",
       "        22.44488978,  22.64529058,  22.84569138,  23.04609218,\n",
       "        23.24649299,  23.44689379,  23.64729459,  23.84769539,\n",
       "        24.04809619,  24.24849699,  24.4488978 ,  24.6492986 ,\n",
       "        24.8496994 ,  25.0501002 ,  25.250501  ,  25.4509018 ,\n",
       "        25.65130261,  25.85170341,  26.05210421,  26.25250501,\n",
       "        26.45290581,  26.65330661,  26.85370741,  27.05410822,\n",
       "        27.25450902,  27.45490982,  27.65531062,  27.85571142,\n",
       "        28.05611222,  28.25651303,  28.45691383,  28.65731463,\n",
       "        28.85771543,  29.05811623,  29.25851703,  29.45891784,\n",
       "        29.65931864,  29.85971944,  30.06012024,  30.26052104,\n",
       "        30.46092184,  30.66132265,  30.86172345,  31.06212425,\n",
       "        31.26252505,  31.46292585,  31.66332665,  31.86372745,\n",
       "        32.06412826,  32.26452906,  32.46492986,  32.66533066,\n",
       "        32.86573146,  33.06613226,  33.26653307,  33.46693387,\n",
       "        33.66733467,  33.86773547,  34.06813627,  34.26853707,\n",
       "        34.46893788,  34.66933868,  34.86973948,  35.07014028,\n",
       "        35.27054108,  35.47094188,  35.67134269,  35.87174349,\n",
       "        36.07214429,  36.27254509,  36.47294589,  36.67334669,\n",
       "        36.87374749,  37.0741483 ,  37.2745491 ,  37.4749499 ,\n",
       "        37.6753507 ,  37.8757515 ,  38.0761523 ,  38.27655311,\n",
       "        38.47695391,  38.67735471,  38.87775551,  39.07815631,\n",
       "        39.27855711,  39.47895792,  39.67935872,  39.87975952,\n",
       "        40.08016032,  40.28056112,  40.48096192,  40.68136273,\n",
       "        40.88176353,  41.08216433,  41.28256513,  41.48296593,\n",
       "        41.68336673,  41.88376754,  42.08416834,  42.28456914,\n",
       "        42.48496994,  42.68537074,  42.88577154,  43.08617234,\n",
       "        43.28657315,  43.48697395,  43.68737475,  43.88777555,\n",
       "        44.08817635,  44.28857715,  44.48897796,  44.68937876,\n",
       "        44.88977956,  45.09018036,  45.29058116,  45.49098196,\n",
       "        45.69138277,  45.89178357,  46.09218437,  46.29258517,\n",
       "        46.49298597,  46.69338677,  46.89378758,  47.09418838,\n",
       "        47.29458918,  47.49498998,  47.69539078,  47.89579158,\n",
       "        48.09619238,  48.29659319,  48.49699399,  48.69739479,\n",
       "        48.89779559,  49.09819639,  49.29859719,  49.498998  ,\n",
       "        49.6993988 ,  49.8997996 ,  50.1002004 ,  50.3006012 ,\n",
       "        50.501002  ,  50.70140281,  50.90180361,  51.10220441,\n",
       "        51.30260521,  51.50300601,  51.70340681,  51.90380762,\n",
       "        52.10420842,  52.30460922,  52.50501002,  52.70541082,\n",
       "        52.90581162,  53.10621242,  53.30661323,  53.50701403,\n",
       "        53.70741483,  53.90781563,  54.10821643,  54.30861723,\n",
       "        54.50901804,  54.70941884,  54.90981964,  55.11022044,\n",
       "        55.31062124,  55.51102204,  55.71142285,  55.91182365,\n",
       "        56.11222445,  56.31262525,  56.51302605,  56.71342685,\n",
       "        56.91382766,  57.11422846,  57.31462926,  57.51503006,\n",
       "        57.71543086,  57.91583166,  58.11623246,  58.31663327,\n",
       "        58.51703407,  58.71743487,  58.91783567,  59.11823647,\n",
       "        59.31863727,  59.51903808,  59.71943888,  59.91983968,\n",
       "        60.12024048,  60.32064128,  60.52104208,  60.72144289,\n",
       "        60.92184369,  61.12224449,  61.32264529,  61.52304609,\n",
       "        61.72344689,  61.9238477 ,  62.1242485 ,  62.3246493 ,\n",
       "        62.5250501 ,  62.7254509 ,  62.9258517 ,  63.12625251,\n",
       "        63.32665331,  63.52705411,  63.72745491,  63.92785571,\n",
       "        64.12825651,  64.32865731,  64.52905812,  64.72945892,\n",
       "        64.92985972,  65.13026052,  65.33066132,  65.53106212,\n",
       "        65.73146293,  65.93186373,  66.13226453,  66.33266533,\n",
       "        66.53306613,  66.73346693,  66.93386774,  67.13426854,\n",
       "        67.33466934,  67.53507014,  67.73547094,  67.93587174,\n",
       "        68.13627255,  68.33667335,  68.53707415,  68.73747495,\n",
       "        68.93787575,  69.13827655,  69.33867735,  69.53907816,\n",
       "        69.73947896,  69.93987976,  70.14028056,  70.34068136,\n",
       "        70.54108216,  70.74148297,  70.94188377,  71.14228457,\n",
       "        71.34268537,  71.54308617,  71.74348697,  71.94388778,\n",
       "        72.14428858,  72.34468938,  72.54509018,  72.74549098,\n",
       "        72.94589178,  73.14629259,  73.34669339,  73.54709419,\n",
       "        73.74749499,  73.94789579,  74.14829659,  74.34869739,\n",
       "        74.5490982 ,  74.749499  ,  74.9498998 ,  75.1503006 ,\n",
       "        75.3507014 ,  75.5511022 ,  75.75150301,  75.95190381,\n",
       "        76.15230461,  76.35270541,  76.55310621,  76.75350701,\n",
       "        76.95390782,  77.15430862,  77.35470942,  77.55511022,\n",
       "        77.75551102,  77.95591182,  78.15631263,  78.35671343,\n",
       "        78.55711423,  78.75751503,  78.95791583,  79.15831663,\n",
       "        79.35871743,  79.55911824,  79.75951904,  79.95991984,\n",
       "        80.16032064,  80.36072144,  80.56112224,  80.76152305,\n",
       "        80.96192385,  81.16232465,  81.36272545,  81.56312625,\n",
       "        81.76352705,  81.96392786,  82.16432866,  82.36472946,\n",
       "        82.56513026,  82.76553106,  82.96593186,  83.16633267,\n",
       "        83.36673347,  83.56713427,  83.76753507,  83.96793587,\n",
       "        84.16833667,  84.36873747,  84.56913828,  84.76953908,\n",
       "        84.96993988,  85.17034068,  85.37074148,  85.57114228,\n",
       "        85.77154309,  85.97194389,  86.17234469,  86.37274549,\n",
       "        86.57314629,  86.77354709,  86.9739479 ,  87.1743487 ,\n",
       "        87.3747495 ,  87.5751503 ,  87.7755511 ,  87.9759519 ,\n",
       "        88.17635271,  88.37675351,  88.57715431,  88.77755511,\n",
       "        88.97795591,  89.17835671,  89.37875752,  89.57915832,\n",
       "        89.77955912,  89.97995992,  90.18036072,  90.38076152,\n",
       "        90.58116232,  90.78156313,  90.98196393,  91.18236473,\n",
       "        91.38276553,  91.58316633,  91.78356713,  91.98396794,\n",
       "        92.18436874,  92.38476954,  92.58517034,  92.78557114,\n",
       "        92.98597194,  93.18637275,  93.38677355,  93.58717435,\n",
       "        93.78757515,  93.98797595,  94.18837675,  94.38877756,\n",
       "        94.58917836,  94.78957916,  94.98997996,  95.19038076,\n",
       "        95.39078156,  95.59118236,  95.79158317,  95.99198397,\n",
       "        96.19238477,  96.39278557,  96.59318637,  96.79358717,\n",
       "        96.99398798,  97.19438878,  97.39478958,  97.59519038,\n",
       "        97.79559118,  97.99599198,  98.19639279,  98.39679359,\n",
       "        98.59719439,  98.79759519,  98.99799599,  99.19839679,\n",
       "        99.3987976 ,  99.5991984 ,  99.7995992 , 100.        ])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_data = np.linspace(0,100,500)\n",
    "y_data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8fb343df",
   "metadata": {},
   "source": [
    "#  等比数列\n",
    "\n",
    "返回在间隔[开始，停止]上计算的num个均匀间隔的样本。<b>数组是一个等比数列构成</b>\n",
    "\n",
    "```python\n",
    "np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)\n",
    "```\n",
    "\n",
    "参数说明   \n",
    "\n",
    "| 序号 | 参数 | 描述说明 |\n",
    "| :--- | :--- | :--- |\n",
    "| 1      | start| 必填项，序列的起始值，  |\n",
    "| 2   | stop | 必填项，序列的终止值，如果endpoint为true，该值包含于数列中      |\n",
    "| 3      | num | 要生成的等步长的样本数量，默认为50      |\n",
    "| 4      | endpoint |  该值为 true 时，数列中包含stop值，反之不包含，默认是True。  | \n",
    "| 5      | base |  对数 log 的底数   | \n",
    "| 6      | dtype |  ndarray 的数据类型 | "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "5ffe6e3b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  1.,   2.,   4.,   8.,  16.,  32.,  64., 128., 256., 512.])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.logspace(0,9,10,base=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8cb06733",
   "metadata": {},
   "source": [
    "np.logspace(A,B,C,base=D)\n",
    "- A: 生成数组的起始值为D的A次方\n",
    "- B:生成数组的结束值为D的B次方\n",
    "- C:总共生成C个数\n",
    "- D:指数型数组的底数为D，当省略base=D时，默认底数为10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "b76e7ebf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2., 4.])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.logspace(0,2,3,base=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "02a5e001",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.        , 1.02869573, 1.05821491, 1.08858117, 1.1198188 ,\n",
       "       1.15195282, 1.18500895, 1.21901365, 1.25399415, 1.28997843,\n",
       "       1.3269953 , 1.36507441, 1.40424622, 1.44454209, 1.48599429,\n",
       "       1.52863599, 1.57250132, 1.61762539, 1.66404434, 1.71179531,\n",
       "       1.76091654, 1.81144733, 1.86342814, 1.91690058, 1.97190744,\n",
       "       2.02849277, 2.08670186, 2.1465813 , 2.20817903, 2.27154434,\n",
       "       2.33672798, 2.4037821 , 2.47276039, 2.54371806, 2.61671192,\n",
       "       2.69180039, 2.76904357, 2.84850331, 2.9302432 , 3.01432868,\n",
       "       3.10082705, 3.18980756, 3.28134142, 3.37550192, 3.47236443,\n",
       "       3.57200647, 3.67450782, 3.77995051, 3.88841897, 4.        ])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.logspace(0,2,base=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "b94517d2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2.,  8., 32.])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 我们先使用前3个参数，将[1,5]均匀分成3个数，得到{1,3,5}，\n",
    "# 然后利用第4个参数base=2(默认是10）使用指数函数可以得到最终输出结果 {2^1,2^3,2^5}\n",
    "np.logspace(1,5,3,base=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "55222273",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 10.        ,  12.91549665,  16.68100537,  21.5443469 ,\n",
       "        27.82559402,  35.93813664,  46.41588834,  59.94842503,\n",
       "        77.42636827, 100.        ])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.logspace(1.0,2.0,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "c64acf1f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.         1.11111111 1.22222222 1.33333333 1.44444444 1.55555556\n",
      " 1.66666667 1.77777778 1.88888889 2.        ]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([ 10.        ,  12.91549665,  16.68100537,  21.5443469 ,\n",
       "        27.82559402,  35.93813664,  46.41588834,  59.94842503,\n",
       "        77.42636827, 100.        ])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.linspace(1.0,2.0,10)\n",
    "print(a)\n",
    "10 ** a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a230802",
   "metadata": {},
   "source": [
    "## 练习题\n",
    "\n",
    "一个穷人到富人那里去借钱，原以为富人不愿意，哪知富人一口答应了下来，\n",
    "但提出了如下条件：\n",
    "- 在30天中，富人第一天借给穷人1万元，第二天借给2万，以后每天所借的钱数都比上一天的多一万；\n",
    "- 但 借钱第一天，穷人还1分钱，第二天还2分钱，以后每天所还的钱数都是上一天的两倍。\n",
    "- 30天后互不相欠，\n",
    "\n",
    "穷人听后觉得挺划算，本想定下来，但又想到富人是个吝啬出了名的，怕上当，所以很为难。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "49f67431",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 10000.,  20000.,  30000.,  40000.,  50000.,  60000.,  70000.,\n",
       "        80000.,  90000., 100000., 110000., 120000., 130000., 140000.,\n",
       "       150000., 160000., 170000., 180000., 190000., 200000., 210000.,\n",
       "       220000., 230000., 240000., 250000., 260000., 270000., 280000.,\n",
       "       290000., 300000.])"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.linspace(10000,300000,30)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "d9db5355",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4650000.0"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = a.sum()\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "a8eb1f33",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10737418.23"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.logspace(0,29,30,base=2) / 100\n",
    "sum = c.sum()\n",
    "sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "3b3c0c13",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6087418.23"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum - b"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f47c95a",
   "metadata": {},
   "source": [
    "#  全0数列\n",
    "\n",
    "创建指定大小的数组，数组元素以 0 来填充\n",
    "\n",
    "```python\n",
    "numpy.zeros(shape, dtype = float, order = 'C')\n",
    "```\n",
    "\n",
    "参数说明   \n",
    "\n",
    "| 序号 | 参数 | 描述说明 |\n",
    "| :--- | :--- | :--- |\n",
    "| 1      | shape| 数组形状  |\n",
    "| 2   | dtype | 数据类型，可选     |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "b3253af9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 0., 0., 0.])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "e45dac86",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 0])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros(5, dtype='int')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "9973f0f9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0],\n",
       "       [0, 0],\n",
       "       [0, 0]])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros((3,2), dtype='int')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "f911ec7c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0, 0],\n",
       "        [0, 0]],\n",
       "\n",
       "       [[0, 0],\n",
       "        [0, 0]],\n",
       "\n",
       "       [[0, 0],\n",
       "        [0, 0]]])"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros((3,2,2), dtype='int')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "0c60b573",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0],\n",
       "       [0, 0, 0]])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1,2,3],(4,5,6)])\n",
    "b = np.zeros_like(a)\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "fcc11caa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array(([1,2,3],[4,5,6]))\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d0104a78",
   "metadata": {},
   "source": [
    "# 全1数列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "d3462733",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.]])"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones([2,4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "f5ab6989",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1],\n",
       "       [1, 1, 1]])"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones((2,3),dtype='int')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "226ef4d1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[1, 1, 1],\n",
       "        [1, 1, 1],\n",
       "        [1, 1, 1]],\n",
       "\n",
       "       [[1, 1, 1],\n",
       "        [1, 1, 1],\n",
       "        [1, 1, 1]]])"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones((2,3,3),dtype='int')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "36fb6182",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([\n",
    "    [\n",
    "        [1,2,3],\n",
    "        [4,5,6]\n",
    "    ],\n",
    "    [\n",
    "        [1,2,3],\n",
    "        [4,5,6]\n",
    "    ]\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "dd243233",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0, 0, 0],\n",
       "        [0, 0, 0]],\n",
       "\n",
       "       [[0, 0, 0],\n",
       "        [0, 0, 0]]])"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.zeros_like(a)\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "e4248787",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "268563fa",
   "metadata": {},
   "source": [
    "# NumPy 数组属性\n",
    "NumPy 的数组中比较重要 ndarray 对象属性有：\n",
    "\n",
    "| 属性             | 说明                                        |\n",
    "| ---------------- | :------------------------------------------ |\n",
    "| ndarray.ndim     | 秩，即轴的数量或维度的数量                  |\n",
    "| ndarray.shape    | 数组的维度，对于矩阵，n 行 m 列             |\n",
    "| ndarray.size     | 数组元素的总个数，相当于 .shape 中 n*m 的值 |\n",
    "| ndarray.dtype    | ndarray 对象的元素类型                      |\n",
    "| ndarray.itemsize | ndarray 对象中每个元素的大小，以字节为单位  |\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f247a8c4",
   "metadata": {},
   "source": [
    "<b>转载[知乎](https://www.zhihu.com/question/64894713)</b>\n",
    "\n",
    "刚学习numpy，对此查找了一下，给出下面的解释，希望能帮到题主。\n",
    "\n",
    "1.ndim中的dim是英文dimension维度的缩写。numpy文档中对ndim的属性见下图解释。<img src=\"https://pica.zhimg.com/50/v2-d9f921131f26de2eb3bab2f6a2fe843f_720w.jpg?source=1940ef5c\" data-rawwidth=\"717\" data-rawheight=\"447\" data-original-token=\"v2-d9f921131f26de2eb3bab2f6a2fe843f\" class=\"origin_image zh-lightbox-thumb\" width=\"717\" data-original=\"https://picx.zhimg.com/v2-d9f921131f26de2eb3bab2f6a2fe843f_r.jpg?source=1940ef5c\"/>因此对于一个数组，其shape属性的长度（length）也既是它的ndim.如下图，一个一维数组c，ndim是1，shape的长度也是1<img src=\"https://pic1.zhimg.com/50/v2-63fe4068aaf3bfa4c8542182cbda2b12_720w.jpg?source=1940ef5c\" data-rawwidth=\"783\" data-rawheight=\"286\" data-original-token=\"v2-63fe4068aaf3bfa4c8542182cbda2b12\" class=\"origin_image zh-lightbox-thumb\" width=\"783\" data-original=\"https://pic1.zhimg.com/v2-63fe4068aaf3bfa4c8542182cbda2b12_r.jpg?source=1940ef5c\"/>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f8313b7f",
   "metadata": {},
   "source": [
    "在下面这个例子中，数组的ndim为2(它有[两个维度](https://www.zhihu.com/search?q=两个维度&search_source=Entity&hybrid_search_source=Entity&hybrid_search_extra={\"sourceType\"%3A\"answer\"%2C\"sourceId\"%3A228169291})).第一个**维度的长度**为2，也就是有两个子数组,第二个维度的长度为3，也就是每个子数组里有三个元素，shape属性返回的是（2，3）\n",
    "\n",
    "```sh\n",
    "[[ 1., 0., 0.],\n",
    " [ 0., 1., 2.]] \n",
    "```\n",
    "\n",
    "你也可以这样来理解：\n",
    "\n",
    "当用数组下标表示的时候，需要用几个数字来表示才能唯一确定这个元素，这个数组就是几维。\n",
    "\n",
    "例如，一维数组a = [1,2,3,4,5,8],取得一个元素用**a[i]，**只有一层\n",
    "\n",
    "下面是一个二维数组，ndim为2，shape属性值为（3，3）\n",
    "\n",
    "```sh\n",
    "b = \n",
    "[\n",
    "   [1,2,3],\n",
    "   [4,5,6],\n",
    "   [7,8,9]\n",
    "]\n",
    "取得一个确切的元素，用b[i][j]的格式,数组嵌套了2层，第一层有三个元素，第二层也是三个元素\n",
    "```\n",
    "\n",
    "下面是一个三维数组，ndim为3，shape属性值为（2，2，3）\n",
    "\n",
    "```sh\n",
    "c = \n",
    "[\n",
    "    [\n",
    "        [1,2,3],\n",
    "        [4,5,6]\n",
    "    ],\n",
    "    [\n",
    "       [7,8,9],\n",
    "       [10,11,12]\n",
    "    ]\n",
    "]\n",
    "取得一个确切的元素，\n",
    "用b[i][j][k]的格式,数组嵌套了3层，第一层有2个元素，第二层也是2个元素,第三层有三个元素\n",
    "```\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db00199a",
   "metadata": {},
   "source": [
    "我根据博主的讲解我总结了一下：\n",
    "\n",
    "1. `ndim`主要是看内嵌了多少个`[]`,一般都习惯是列表，而不是用元组，故只需观察内嵌的中括号个数就是`ndim`的数量\n",
    "2. `shape`主要是逐层分解中括号，去掉一层在看内部有多少个元素。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "691fa62d",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([\n",
    "    [\n",
    "        [1,2,3],\n",
    "        [4,5,6],\n",
    "        [7,8,9]\n",
    "    ],\n",
    "    [\n",
    "        [1,2,3],\n",
    "        [4,5,6],\n",
    "        [7,8,9]\n",
    "    ]\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "eb7cc6b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.ndim # 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "7f19cc2e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 3, 3)"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.shape # 2，3，3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "5f839b29",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "18"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.size # 2*3*3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "87a04442",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int64')"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "1fcf6c5c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.itemsize"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ce00999",
   "metadata": {},
   "source": [
    "#### 调整维度 reshape\n",
    "返回调整维度后的副本，而不改变原 ndarray。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "9fab4f18",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,\n",
       "       18, 19, 20])"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(1,20+1)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "710a88d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = a.reshape((4,5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "e2ea5705",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a: [ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20]\n",
      "b: [[ 1  2  3  4  5]\n",
      " [ 6  7  8  9 10]\n",
      " [11 12 13 14 15]\n",
      " [16 17 18 19 20]]\n"
     ]
    }
   ],
   "source": [
    "print('a:',a)\n",
    "print('b:',b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d84fa805",
   "metadata": {},
   "source": [
    "### 调整维度 resize\n",
    "`numpy.resize(a, new_shape)`\n",
    "如果新数组大于原始数组，则新数组将填充a的重复副本。\n",
    "> 请注意，此行为与a.resize(new_shape)不同，后者用零而不是重复的a填充。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "808e597d",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([\n",
    "    [1,2],\n",
    "    [3,4]\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "70dcf23a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 1, 2]])"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.resize(a,(2,3))\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "a847d38a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4]])"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16cc51e5",
   "metadata": {},
   "source": [
    "☝️上面的会先展开，然后在填充"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "11b689af",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3, 4, 0, 0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.resize((5,10),refcheck = False) # 用零而不是重复的a填充。\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ff73bb0",
   "metadata": {},
   "source": [
    "#### 方法astype()\n",
    "numpy数据类型转换，调用astype返回数据类型修改后的数据，但是源数据的类型不会变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "ef92e13e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = a.astype('float64')\n",
    "c.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "d3653431",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int64')"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.dtype"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3701ef2",
   "metadata": {},
   "source": [
    "### ndarray.itemsize\n",
    "以字节的形式返回数组中每一个元素的大小。\n",
    "\n",
    "例如，一个元素类型为 float64 的数组 itemsize 属性值为 8(float64 占用 64 个 bits，每个字节长度为 8，所以 64/8，占用 8 个字节）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "51d452f2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dtype: float64  itemsize: 8\n",
      "dtype: <U21  itemsize: 84\n"
     ]
    }
   ],
   "source": [
    "a = np.array([1.1,2.2,3.3])\n",
    "print('dtype:',a.dtype,' itemsize:',a.itemsize)\n",
    "\n",
    "b = np.array([1,2,3,4,5]).astype('str_')\n",
    "print('dtype:',b.dtype,' itemsize:',b.itemsize)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57becac0",
   "metadata": {},
   "source": [
    "# 数据类型\n",
    "\n",
    "| 名称           | 描述                                                         | 名称          | 描述                                                         |\n",
    "| -------------- | :----------------------------------------------------------- | ------------- | :----------------------------------------------------------- |\n",
    "| bool_          | 布尔型数据类型（True 或者 False）                            | float_        | float64 类型的简写                                           |\n",
    "| int_           | 默认的整数类型（类似于 C 语言中的 long，int32 或 int64）     | float16/32/64 | 半精度浮点数:1 个符号位，5 个指数位，10个尾数位<br/>单精度浮点数:1 个符号位，8 个指数位，23个尾数位<br/>双精度浮点数,包括：1 个符号位，11 个指数位，52个尾数位 |\n",
    "| intc           | 和 C 语言的 int 类型一样，一般是 int32 或 int 64             | complex_      | 复数类型，与 complex128 类型相同                             |\n",
    "| intp           | 用于索引的整数类型（类似于 C 的 ssize_t，通常为 int32 或 int64） | complex64/128 | 复数，表示双 32 位浮点数（实数部分和虚数部分）<br/>复数，表示双 64 位浮点数（实数部分和虚数部分） |\n",
    "| int8/16/32/64  | 代表与1字节相同的8位整数<br/>代表与2字节相同的16位整数<br/>代表与4字节相同的32位整数<br/>代表与8字节相同的64位整数 | str_          | 表示字符串类型                                               |\n",
    "| uint8/16/32/64 | 代表1字节（8位）无符号整数<br/>代表与2字节相同的16位整数<br/>代表与4字节相同的32位整数<br/>代表与8字节相同的64位整数 | string_       | 表示字节串类型,也就是bytes类型                               |\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python(numpy_learning)",
   "language": "python",
   "name": "numpy_learning"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
